﻿using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Animation;

namespace BankerenClient
{
    public class CardPanel : Panel
    {
        #region Properties

        [Category("CardPanel")]
        public int PopUp
        {
            get { return (int)GetValue(PopUpProperty); }
            set { SetValue(PopUpProperty, value); }
        }

        [Category("CardPanel")]
        public int Overlay
        {
            get { return (int)GetValue(OverlayProperty); }
            set { SetValue(OverlayProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Popup.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty PopUpProperty =
            DependencyProperty.Register("PopUp", typeof(int), typeof(CardPanel), new UIPropertyMetadata(0));

        // Using a DependencyProperty as the backing store for Overlay.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty OverlayProperty =
            DependencyProperty.Register("Overlay", typeof(int), typeof(CardPanel), new UIPropertyMetadata(0));

        #endregion

        #region Methods

        protected override Size MeasureOverride(Size availableSize)
        {
            double maxHeight = 0;
            double totalWidth = 0;

            for (int i = 0; i < InternalChildren.Count; i++)
            {
                UIElement child = InternalChildren[i];
                child.Measure(availableSize);
                Size desiredSize = child.DesiredSize;
                if (i < InternalChildren.Count - 1)
                {
                    maxHeight = Math.Max(desiredSize.Height, maxHeight);
                    totalWidth += Overlay;
                }
                else
                {
                    maxHeight = Math.Max(desiredSize.Height, maxHeight);
                    totalWidth += desiredSize.Width;
                }
            }

            return new Size(totalWidth + 3, maxHeight);
        }

        protected override Size ArrangeOverride(Size finalSize)
        {
            int i = 0;
            TranslateTransform trans = null;
            foreach (UIElement child in Children)
            {
                trans = child.RenderTransform as TranslateTransform;
                if (trans == null)
                {
                    child.RenderTransformOrigin = new Point(0, 0);
                    trans = new TranslateTransform();
                    child.RenderTransform = trans;
                }
                int curX = i * Overlay;
                child.Arrange(new Rect(0, 0, child.DesiredSize.Width, child.DesiredSize.Height));
                trans.BeginAnimation(TranslateTransform.XProperty, new DoubleAnimation(curX, TimeSpan.FromMilliseconds(700)), HandoffBehavior.Compose);
                i++;
            }

            return finalSize;
        }

        protected override void OnVisualChildrenChanged(DependencyObject visualAdded, DependencyObject visualRemoved)
        {
            base.OnVisualChildrenChanged(visualAdded, visualRemoved);

            //For every element added 
            //add an event handler to know when it is clicked
            UIElement element = visualAdded as UIElement;
            if (element != null)
            {
                element.AddHandler(UIElement.MouseEnterEvent, new RoutedEventHandler(element_MouseEnter), true);
                element.AddHandler(UIElement.MouseLeaveEvent, new RoutedEventHandler(element_MouseLeave), true);
            }
        }

        private void element_MouseEnter(object sender, RoutedEventArgs e)
        {
            UIElement child = (UIElement)sender;
            TranslateTransform scale = (TranslateTransform)child.RenderTransform;
            {
                scale.BeginAnimation(TranslateTransform.YProperty, new DoubleAnimation(-5, TimeSpan.FromMilliseconds(100)));
            }
        }

        private void element_MouseLeave(object sender, RoutedEventArgs e)
        {
            UIElement child = (UIElement)sender;
            TranslateTransform scale = (TranslateTransform)child.RenderTransform;
            {
                scale.BeginAnimation(TranslateTransform.YProperty, new DoubleAnimation(0, TimeSpan.FromMilliseconds(100)));
            }
        }


        #endregion
    }
}
